#include "taskFlyport.h"
#include "Include/mcp2515.h"



unsigned char buf[100];
int cnf1, cnf2,cnf3; //extern variables to make them visible to HTTPApp.c


#define CNF3		0x28
#define CNF2		0x29
#define CNF1		0x2A 


void FlyportTask()
{
	WFConnect(WF_DEFAULT);
	while (WFStatus != CONNECTED);
	UARTWrite(1,"Flyport connected... hello world!\r\n");
	
	//SPIInit();
	MCPInit();
	//IOPut(4, OFF);
	//SPI2BUF = 0b00000011;	
	//SPI2BUF = 0x0F;
	//DelayMs(10);
	
	unsigned char adress = 0;
	
	while(1)
	{
		
		/*
		int cnf1 = MCPReadRegister(CNF1);
		int cnf2 = MCPReadRegister(CNF2);
		int cnf3 = MCPReadRegister(CNF3);
		*/
		
		/*cnf1 = MCPReadRegister(CNF1);
		cnf2 = MCPReadRegister(CNF2);
		cnf3 = MCPReadRegister(CNF3);
		unsigned char a = MCPReadRegister(0x62);
		unsigned char b = MCPReadRegister(0x66);
		unsigned char c = MCPReadRegister(0x1D);
		unsigned char d = MCPReadRegister(0x2D);
		unsigned char e = MCPReadRegister(0x30);
		
		sprintf(buf, "cnf1: %i cnf2: %i cnf3: %i rxb0sidl: %i rxb0: %i err: %i errflg: %i txb0ctrl: %i\r\n", cnf1, cnf2, cnf3, a, b, c, d, e);
		UARTWrite(1, buf);
		DelayMs(250);*/
		/*
		CanPacket packet;
		unsigned int newpacket = MCPReceivePacket(&packet);

		sprintf(buf, "A newpacket %i\r\n", newpacket);
		UARTWrite(1, buf);
		DelayMs(250);
		*/
		INT baud = AutoBaud(0, 16, 1);
		
		sprintf(buf, "Baud Rate: %i\r\n", baud);
		UARTWrite(1, buf);
		////MCPSendPacket(packet);
		
/*
		newpacket = MCPReceivePacket(&packet);
		sprintf(buf, "B newpacket %i\r\n", newpacket);
		UARTWrite(1, buf);
		sprintf(buf, "%x : %x %x %x %x %x %x %x %x\r\n", packet.numData, packet.data[0], packet.data[1], packet.data[2], packet.data[3],
													packet.data[4], packet.data[5], packet.data[6], packet.data[7]);
		UARTWrite(1, buf);
		DelayMs(250);
		*/
		//DelayMs(500);
	}
}
